/**
 * 加密
 * @param val String
 * @return {string|null}
 */
const create = (val) => {
    if (!val && val !== 0) return null;
    const strVal = val.toString();
    const codeArr = [];
    for (let i = 0; i < strVal.length; i += 1) {
        codeArr.push(strVal[i].charCodeAt().toString(16));
    }
    return encodeURIComponent(codeArr.join(','));
};

/**
 * 解密
 * @param val String
 * @return {string|null}
 */
const parser = (val) => {
    if (!val && val !== 0) return null;
    const strVal = decodeURIComponent(val.toString());
    const codeArr = strVal.split(',');
    let str = '';
    for (let i = 0; i < codeArr.length; i += 1) {
        str += String.fromCharCode(parseInt(codeArr[i], 16));
    }
    return str;
};

export { create, parser };
